﻿using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AndreySmirnov.BLToolkitUtils.VSPackage.Model.SchemaProvider;

namespace AndreySmirnov.BLToolkitUtils.VSPackage.CodeGeneration
{
    public static class CodeGenerationHelpers 
    {
        private static readonly CodeTypeReference s_generatedCodeAttr = new CodeTypeReference(typeof(GeneratedCodeAttribute));

        public static void AddGeneratedCodeAttribute(this CodeAttributeDeclarationCollection collection)
        {
            collection.Add(CreateGeneratedCodeAttribute());
        }

        public static CodeAttributeDeclaration CreateGeneratedCodeAttribute()
        {
            return new CodeAttributeDeclaration(s_generatedCodeAttr,
                new CodeAttributeArgument(new CodePrimitiveExpression("BLToolkitUtilsPackage")),
                new CodeAttributeArgument(new CodePrimitiveExpression(BLToolkitUtilsPackage.CURRENT_VERSION)));
        }

        public static string CreateValidIdentifier(this string identifier, CodeDomProvider domProvider)
        {
            var str = identifier.Replace(" ", string.Empty);
            return domProvider.CreateValidIdentifier(str);
        }

        public static string CreateValidIdentifier(this TableInfo tableInfo, CodeDomProvider domProvider)
        {
            var str = 
                (tableInfo.IsDefaultSchema 
                ? tableInfo.TableName
                : string.Format("{0}.{1}", tableInfo.SchemaName, tableInfo.TableName))
                    .Replace(" ", string.Empty);
            return domProvider.CreateValidIdentifier(str);
        }

        public static string GetTableClassNameWithRelativeNamespace(this TableInfo tableInfo, CodeDomProvider domProvider)
        {
            var validName = tableInfo.TableName.CreateValidIdentifier(domProvider);
            if (tableInfo.IsDefaultSchema)
                return validName;
            return string.Format( "{0}.{1}", tableInfo.SchemaName.CreateValidIdentifier(domProvider), validName);
        }

        public static string GetTableClassNameWithAbsoluteNamespace(this TableInfo tableInfo, string rootNamespace, CodeDomProvider domProvider)
        {
            var validName = tableInfo.TableName.CreateValidIdentifier(domProvider);
            if (tableInfo.IsDefaultSchema)
                return string.Format("{0}.{1}", rootNamespace, validName);
            return string.Format( "{0}.{1}.{2}", 
                rootNamespace,
                tableInfo.SchemaName.CreateValidIdentifier(domProvider), 
                validName);
        }

        public static string GetTableAbsoluteNamespace(this TableInfo tableInfo, string rootNamespace, CodeDomProvider domProvider)
        {
            if (tableInfo.IsDefaultSchema)
                return rootNamespace;
            return string.Format( "{0}.{1}", 
                rootNamespace,
                tableInfo.SchemaName.CreateValidIdentifier(domProvider));
        }

        public static string GetFullTableName(this TableInfo tableInfo)
        {
            return string.Format("[{0}].[{1}]", tableInfo.SchemaName, tableInfo.TableName);
        }
    }
}
